package cn.iocoder.yudao.module.pay.framework;

import cn.iocoder.yudao.framework.util.SwaggerUtils;
import cn.iocoder.yudao.module.api.pay.order.eums.UnifiedPayChannelEnum;
import cn.iocoder.yudao.module.pay.channel.service.PayChannelService;
import cn.iocoder.yudao.module.pay.channel.service.PayChannelServiceImpl;
import cn.iocoder.yudao.module.pay.channel.vo.WxApiV3ConfigRespVO;
import cn.iocoder.yudao.module.pay.framework.impl.NonePayClientConfig;
import cn.iocoder.yudao.module.pay.framework.impl.PayClientFactoryImpl;
import cn.iocoder.yudao.module.pay.framework.impl.cash.CashPayClient;
import cn.iocoder.yudao.module.pay.framework.impl.free.FreePayClient;
import cn.iocoder.yudao.module.pay.framework.impl.mallbook.MallBookPayClientConfig;
import cn.iocoder.yudao.module.pay.framework.impl.mallbook.MallBookWxLitePayClient;
import cn.iocoder.yudao.module.pay.framework.impl.mock.MockPayClient;
import cn.iocoder.yudao.module.pay.framework.impl.wallet.WalletPayClient;
import cn.iocoder.yudao.module.pay.framework.impl.weixin.WxLitePayClient;
import cn.iocoder.yudao.module.pay.framework.impl.weixin.WxPayClientConfig;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 配置类
 */
@Configuration(proxyBeanMethods = false)
public class PayConfiguration {

    @Bean
    public GroupedOpenApi payGroupedOpenApi() {
        return SwaggerUtils.buildGroupedAppApi("pay");
    }

    @Bean
    public PayClientFactory payClientFactory() {
        return new PayClientFactoryImpl();
    }

    @Bean
    public PayChannelService payChannelService() {
        return new PayChannelServiceImpl();
    }

    @Bean
    public MockPayClient getMockPayClient() {
        return new MockPayClient(new NonePayClientConfig());
    }

    @Bean
    public CashPayClient getCashPayClient() {
        return new CashPayClient(new NonePayClientConfig());
    }

    @Bean
    public FreePayClient getFreePayClient() {
        return new FreePayClient(new NonePayClientConfig());
    }

    @Bean
    public WalletPayClient getWalletPayClient() {
        return new WalletPayClient(new NonePayClientConfig());
    }

    @Bean
    public WxLitePayClient getWxLitePayClient(PayChannelService payChannelService, PayClientFactory payClientFactory) {
        WxApiV3ConfigRespVO configRespVO = payChannelService.getWxApiV3Config(UnifiedPayChannelEnum.WX_LITE.getCode());
        WxPayClientConfig config = new WxPayClientConfig();
        config.setAppId(configRespVO.getAppId());
        config.setMchId(configRespVO.getMchId());
        config.setApiVersion(WxPayClientConfig.API_VERSION_V3);
        config.setApiV3Key(configRespVO.getSecretKey());
        config.setPrivateKeyContent(configRespVO.getPrivateKeyContent());
        config.setPrivateCertContent(configRespVO.getPrivateCertContent());
        return payClientFactory.createOrUpdatePayClient(UnifiedPayChannelEnum.WX_LITE.getCode(), config);
    }

    @Bean
    public MallBookWxLitePayClient getMallBookWxLitePayClient(PayClientFactory payClientFactory) {
        MallBookPayClientConfig config = MallBookPayClientConfig.Holder.INSTANCE;
        return payClientFactory.createOrUpdatePayClient(UnifiedPayChannelEnum.MALLBOOK_WX_LITE.getCode(), config);
    }

}
